package com.foreknow.example8;

import java.util.HashMap;
import java.util.Map;

public class Test4 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 2, 1, 2, 3, 3, 3};
        Map<Integer, Integer> frequencyMap = new HashMap<>();

        // 统计元素出现次数
        for (int num : nums) {
            frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
        }

        // 找到出现次数最多的元素
        int mostFrequentElement = 0;
        int maxFrequency = 0;
        for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
            if (entry.getValue() > maxFrequency) {
                mostFrequentElement = entry.getKey();
                maxFrequency = entry.getValue();
            }
        }

        System.out.println("Most frequent element: " + mostFrequentElement);
        System.out.println("Frequency: " + maxFrequency);
    }
}
